<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class series extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	var $model = "mseries";
	var $view = "series";
	var $resource = "";
    function __construct() {
        parent::__construct();
        $this->load->model($this->model);
        $this->load->model('mgameseries');
		$this->resCode = $this->{$this->model}->resCode;
        $this->url_index = admin_url() . $this->view;
        $this->folder_upload = "dataupload/".$this->view."/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
		$this->resource = $this->mresource->get($this->resCode,'RES_CODE');
    }

    public function index($page = 0) {
		$this->check_permission();
        $data = array();
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 20;
        $config['cur_page'] = $page;
        $config['base_url'] = $this->url_index.'/index/';
        $config['total_rows'] = $this->{$this->model}->num();
        $this->pagination->initialize($config);
        $data['datas'] = $this->{$this->model}->pagination('','',$config);
        $this->theme_data["main"] = $this->view."/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($editID = 0) {
		$this->check_permission($editID);
		$this->load->model("simage");
        $data = array();
        $post = $this->input->post();
        $data['edit'] = $this->{$this->model}->get($editID);
        if ($post) {
			$post['edit']['series_time_update'] = time();
			if($post['edit']['series_code'] ==''){
				$post['edit']['series_code'] = $this->sseo->seolink($post['edit']['series_title']);
			}
			if($upload = $this->upload(@$data['edit']->series_thumb)){
				$post['edit']['series_thumb'] = $this->folder_upload.$upload['upload_data']['file_name'];
				$this->simage->cache($post['edit']['series_thumb']);
			}
            if ($editID > 0) {
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				$editID = $this->{$this->model}->insert($post['edit']);
			}
			$sgame_id = explode("|",$this->input->post("lists_games"));
				$this->mgameseries->update_game($sgame_id, $editID);
            redirect($this->url_edit . $editID);
        }
		$this->db->select('game.game_id, game.game_title');
		$data['games'] = $this->mgameseries->gets_game($editID);
		$lists_games = array();
		foreach($data['games'] as $game){
			$lists_games[] = $game->game_id;
		}
		$data['lists_games'] = implode("|",$lists_games);
        $this->theme_data["main"] = $this->view."/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function ajax_search_game(){
		$data = array();
		$post = $this->input->get();
		if(isset($post['q'])){
			$this->db->select("game_id, game_title");
			$this->db->from("game");
			$this->db->where("game_status >", 0);
			$this->db->like("game_title", $post['q']);
			$this->db->limit(10);
			$games = $this->db->get()->result();
			if($games)
			foreach($games as $game){
				$data[] = array(
					'id'=> $game->game_id,
					'title'=> $game->game_title,
				);
			}
		}
		die(json_encode($data));
	}
	function upload($old_name = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		if(!is_dir($this->folder_upload))
		mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
	}
}
